#ifndef UTI_INSTDB_H_
#define UTI_INSTDB_H_
#pragma once
#include "sys_config.h"


#include "KompexSQLitePrerequisites.h"
#include "KompexSQLiteDatabase.h"
#include "KompexSQLiteStatement.h"
#include "KompexSQLiteException.h"
#include "KompexSQLiteStreamRedirection.h"
#include "KompexSQLiteBlob.h"

#include <iostream>
#include <exception>
using namespace Kompex;
#pragma warning(disable : 4996)

class CInstDB
{
public:

	//DB(BarInfo* pbar, BarSeries_broad** ppbarserbroad, unsigned int cnt);

	CInstDB(char* tradingday);

	~CInstDB();

	bool WriteData(map<string, vector<InstInfo> >& instWrite);

	bool ReadData(map<string, vector<InstInfo> >& instRead);

private:


	bool InitDBInfo_w();

	void DestroyDBInfo_w();

	bool InitDBInfo_r();

	void DestroyDBInfo_r();

	bool InitBuffer();

	void DestroyBuffer();

	bool CheckDBFile();





	bool OpenDB_w();

	bool WriteDB_w(map<string, vector<InstInfo> >& instWrite);

	bool CloseDB_w();





	bool OpenDB_r();

	bool ReadDB_r(map<string, vector<InstInfo> >& instRead);

	bool CloseDB_r();


	//bool UpdateDB(unsigned int index);

	//bool InsertDB(unsigned int index);

	//bool UpdateCSV(unsigned int index);

	//bool InsertCSV(unsigned int index);

	//bool UpdateMemDB(unsigned int index);




	//bool OnFireConditon();

	//bool SetDBLastbar(unsigned int index);



	//bool InsertDB_TEST(unsigned int index, unsigned int base);


private:

	typedef struct DBINFO
	{
		DBINFO(string& _dbname, string& _tbname, string& _dbf)
		{

			pdb = NULL;
			pst = NULL;

			dbname = _dbname;
			tbname = _tbname;

			dbfolder =  _dbf;


		};

		string dbname;
		string tbname;
		
		SQLiteDatabase* pdb;
		SQLiteStatement* pst;

		
		string dbfolder;


	}DBINFO;


	DBINFO* m_pdbinfo_w;

	DBINFO* m_pdbinfo_r;

	char* m_buffer;

	char m_tradingday[20];



};





#endif